using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace YY.Basic
{
	public class ResourceHelper
	{ 
		public static Cursor LoadCursor( string cursorName)
		{
			// Get the assembly that contains the bitmap resource
            Assembly myAssembly = Assembly.GetAssembly(Type.GetType("YY.Basic.ResourceHelper"));

			// Get the resource stream containing the images
			Stream iconStream = myAssembly.GetManifestResourceStream(cursorName);

			// Load the Icon from the stream
			return new Cursor(iconStream);
		}
    
		public static Icon LoadIcon( string iconName)
		{
			// Get the assembly that contains the bitmap resource
            Assembly myAssembly = Assembly.GetAssembly(Type.GetType("YY.Basic.ResourceHelper"));

			// Get the resource stream containing the images
			Stream iconStream = myAssembly.GetManifestResourceStream(iconName);

			// Load the Icon from the stream
			return new Icon(iconStream);
		}

		public static Icon LoadIcon( string iconName, Size iconSize)
		{
			// Load the entire Icon requested (may include several different Icon sizes)
			Icon rawIcon = LoadIcon( iconName);
			
			// Create and return a new Icon that only contains the requested size
			return new Icon(rawIcon, iconSize); 
		}

		public static Bitmap LoadBitmap(string imageName)
		{
            return LoadBitmap(Type.GetType("YY.Basic.ResourceHelper"), imageName, false, new Point(0, 0));
		}

		public static Bitmap LoadBitmap(string imageName, Point transparentPixel)
		{
            return LoadBitmap(Type.GetType("YY.Basic.ResourceHelper"), imageName, true, transparentPixel);
		}

		
		public static Image LoadImage(string imageName) 
		{
            return LoadBitmap(Type.GetType("YY.Basic.ResourceHelper"), imageName, false, new Point(0, 0));
		}

		public static ImageList LoadBitmapStrip(string imageName, Size imageSize)
		{
            return LoadBitmapStrip(Type.GetType("YY.Basic.ResourceHelper"), imageName, imageSize, false, new Point(0, 0));
		}

		public static ImageList LoadBitmapStrip( 
			string imageName, 
			Size imageSize,
			Point transparentPixel)
		{
            return LoadBitmapStrip(Type.GetType("YY.Basic.ResourceHelper"), imageName, imageSize, true, transparentPixel);
		}

		protected static Bitmap LoadBitmap(Type assemblyType, 
			string imageName, 
			bool makeTransparent, 
			Point transparentPixel)
		{
			// Get the assembly that contains the bitmap resource
			Assembly myAssembly = Assembly.GetAssembly(assemblyType);

			// Get the resource stream containing the images
			Stream imageStream = myAssembly.GetManifestResourceStream(imageName);

			// Load the bitmap from stream
			Bitmap image = new Bitmap(imageStream);

			if (makeTransparent)
			{
				Color backColor = image.GetPixel(transparentPixel.X, transparentPixel.Y);
    
				// Make backColor transparent for Bitmap
				image.MakeTransparent(backColor);
			}
			    
			return image;
		}

		protected static ImageList LoadBitmapStrip(Type assemblyType, 
			string imageName, 
			Size imageSize,
			bool makeTransparent,
			Point transparentPixel)
		{
			// Create storage for bitmap strip
			ImageList images = new ImageList();

			// Define the size of images we supply
			images.ImageSize = imageSize;

			// Get the assembly that contains the bitmap resource
			Assembly myAssembly = Assembly.GetAssembly(assemblyType);

			// Get the resource stream containing the images
			Stream imageStream = myAssembly.GetManifestResourceStream(imageName);

			// Load the bitmap strip from resource
			Bitmap pics = new Bitmap(imageStream);

			if (makeTransparent)
			{
				Color backColor = pics.GetPixel(transparentPixel.X, transparentPixel.Y);
    
				// Make backColor transparent for Bitmap
				pics.MakeTransparent(backColor);
			}
			    
			// Load them all !
			images.Images.AddStrip(pics);

			return images;
		}

	}
}
